package booroo.basic;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class J19_CDinfo {

	// 등록,연결,sql문,출력,종료
	// JDBC접속정보
	private static final String DRV = "oracle.jdbc.OracleDriver";
	private static final String URL = "jdbc:oracle:thin:@//localhost:1521/xe";
	private static final String USR = "scott";
	private static final String PWD = "TIGER";
	
	// SQL문 작성
	private static final String createSQL = "create table cdinfo(cd_id char(5) primary key not null,title varchar(50),publisher varchar(30),artist varchar(20),price number(5))";
	private static final String insertSQL = "insert into cdinfo values('a0001','홍길동1집','오늘 레코드','홍길동',8000)";
	private static final String selectSQL = "select title from cdinfo where price=8000";
	
	public static void main(String[] args) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
						
		try{
			Class.forName(DRV);
			conn = DriverManager.getConnection(URL,USR,PWD);
			ps = conn.prepareStatement(createSQL);
			int cnt = ps.executeUpdate();
			System.out.println(cnt + "개 테이블 생성");
			ps.close();
			
			ps = conn.prepareStatement(insertSQL);
			cnt = ps.executeUpdate();
			System.out.println(cnt + "개 레코드 입력");
			ps.close();
			
			ps = conn.prepareStatement(selectSQL);
			rs = ps.executeQuery();
			while(rs.next()){
			System.out.println(rs.getString(1));
			}
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		finally{
			if(rs != null)
				try{rs.close();}catch(Exception e){}
			if(ps != null)
				try{ps.close();}catch(Exception e){}
			if(conn != null)
				try{conn.close();}catch(Exception e){}
		}
						
	}

}
